local shilun = fk.CreateSkill{
    name = "wk_heg__shilun",
}

Fk:loadTranslationTable{
    ["wk_heg__shilun"] = "世论",
    [":wk_heg__shilun"] = "当你受到伤害后，你可展示所有手牌并弃至每种花色各一张，然后若你的手牌：包含四种花色，你可移动场上一张牌；不包含四种花色，你从牌堆中检索并获得手牌中没有的花色牌各一张。",

    ["#wk_heg__shilun_active-choose"] = "世论：选择花色各不相同的手牌各一张",
    ["#wk_heg__shilun-move"] = "世论：你可以移动场上一张牌",

    ["$wk_heg__shilun1"] = "某有良谋，可为将军所用。",
    ["$wk_heg__shilun2"] = "吾负十斗之囊，其盈一石之智。",
}

shilun:addEffect(fk.Damaged,{
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
      return player == target and player:hasSkill(shilun.name) and not player:isKongcheng()
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = player.player_cards[Player.Hand]
    player:showCards(cards)
    local _, ret = room:askToUseActiveSkill(player, {
        skill_name = "#wk_heg__shilun_active",
        prompt = "#wk_heg__shilun_active-choose",
        cancelable = false,
    })
    local to_remain = {}
    if ret then
      to_remain = ret.cards
    else
      local suits = {}
      for _, cid in ipairs(player:getCardIds{Player.Hand}) do
        local c = Fk:getCardById(cid)
        if not table.contains(suits, c.suit) then
          table.insert(suits, c.suit)
          table.insert(to_remain, cid)
        end
      end
    end
    local cards1 = table.filter(player:getCardIds{Player.Hand}, function (id)
      return not (table.contains(to_remain, id) or player:prohibitDiscard(Fk:getCardById(id)))
    end)
    if #cards1 > 0 then
      room:throwCard(cards1, shilun.name, player)
    end
    local cards2 = player.player_cards[Player.Hand]
    if #cards2 == 4 then
      if #room:canMoveCardInBoard() > 0 then
        local targets = room:askToChooseToMoveCardInBoard(player,{
            prompt = "#wk_heg__shilun-move",
            skill_name = shilun.name,
            cancelable = true,
        })
        if #targets ~= 0 then
          room:askToMoveCardInBoard(player,{
            target_one = targets[1],
            target_two = targets[2],
            skill_name = shilun.name,
        })
        end
      end
    else
      local suits = {Card.Spade, Card.Heart, Card.Diamond, Card.Club}
      for _, id in ipairs(cards) do
        local suit = Fk:getCardById(id).suit
        if suit ~= Card.NoSuit then
          table.removeOne(suits, suit)
        end
      end
      local patternTable = { ["heart"] = {}, ["diamond"] = {}, ["spade"] = {}, ["club"] = {} }
      for _, id in ipairs(room.draw_pile) do
        local card = Fk:getCardById(id)
        if table.contains(suits, card.suit) then
          table.insert(patternTable[card:getSuitString()], id)
        end
      end
      local get = {}
      for _, ids in pairs(patternTable) do
        if #ids > 0 then
          table.insert(get, table.random(ids))
        end
      end
      if #get > 0 then
        room:obtainCard(player, get, false, fk.ReasonPrey, player, shilun.name)
      end
    end
  end,
})

return shilun